package test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import uci.rt.ccr.engine.Utils;
import uci.rt.ccr.engine.matrix.Mat;
import au.com.bytecode.opencsv.CSVReader;

public class Nio
{

	public static void main(String[] args) throws IOException
	{
		//ReadTest();
		WriteTest();
		

	}

	private static void WriteTest()
	{
		long start = 0;
		long finish = 0;
		String val="abcdefghijklmnopqrtuvz 012345679";
		ArrayList<String> array = new ArrayList<String>();
		for(int i=0;i<2e6;i++){
			array.add(i+") "+val+"\n");
		}

		start = System.currentTimeMillis();
		Utils.writeArrayToFile("NioTest.txt", array);
		finish = System.currentTimeMillis();
		System.out.println("writeArrayToFile :" + (finish - start) + " milliseconds");
		
	}

	private static void ReadTest() throws IOException
	{

		String file = "sqlout987opt";
		int n = 100;
		double[][] times = new double[n][1];

		ArrayList<String> listNio = null;
		long start = 0;
		long finish = 0;


		/*for (int i = 0; i < n; i++)
		{
			start = System.currentTimeMillis();
			listNio = readNio4(file);
			finish = System.currentTimeMillis();
			times[i][1] = (finish - start);
		}
		System.out.println("readNio4 :" + (finish - start) + " milliseconds");

		for (int i = 0; i < n; i++)
		{
			start = System.currentTimeMillis();
			listNio = Utils.readTxtFile(file);
			finish = System.currentTimeMillis();
			times[i][2] = (finish - start);
		}
		System.out.println("readTxtFile :" + (finish - start) + " milliseconds");*/

		for (int i = 0; i < n; i++)
		{
			start = System.currentTimeMillis();
			listNio = mix4(file);
			finish = System.currentTimeMillis();
			times[i][0] = (finish - start);
		}
		System.out.println("mix4 :" + (finish - start) + " milliseconds");
		
		/*for (int i = 0; i < n; i++)
		{
			start = System.currentTimeMillis();
			listNio = bufferedInputStream(file);
			finish = System.currentTimeMillis();
			times[i][3] = (finish - start);
		}
		System.out.println("bufferedInputStream :" + (finish - start) + " milliseconds");*/
		Mat t=new Mat(times);
		t.writeAsCsvFile("timeReadWrite", ",");
		
		
	}

	public static ArrayList<String> readNio4(String file)
	{

		FileInputStream fIn;
		FileChannel fChan;
		long fSize;
		ByteBuffer mBuf;
		ArrayList<String> list = new ArrayList<String>();

		try
		{
			fIn = new FileInputStream(file);
			fChan = fIn.getChannel();
			fSize = fChan.size();
			mBuf = ByteBuffer.allocate((int) fSize);
			fChan.read(mBuf);
			mBuf.rewind();
			StringBuilder lineSB = new StringBuilder();
			for (int i = 0; i < fSize; i++)
			{
				int c = mBuf.get();
				lineSB.append((char) c);
				if (c == 10)
				{
					String s = lineSB.toString();
					list.add(s.substring(0, s.length() - 2));
					lineSB = new StringBuilder();
				}
			}
			fChan.close();
			fIn.close();
		}
		catch (IOException exc)
		{
			System.out.println(exc);
		}
		return list;
	}

	public static ArrayList<String> kcsmf(String file) throws IOException
	{
		ArrayList<String> list = new ArrayList<String>();

		FileInputStream in = new FileInputStream(file);
		FileChannel channel = in.getChannel();
		int lenghth = (int) channel.size();
		MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, lenghth);
		StringBuilder lineSB = new StringBuilder();
		for (int p = 0; p < lenghth; p++)
		{
			if (p % 1000000 == 0)
				System.out.println(p);
			int c = buffer.get(p);
			lineSB.append((char) c);
			if (c == 10)
			{
				String s = lineSB.toString();
				list.add(s.substring(0, s.length() - 2));
				lineSB = new StringBuilder();
			}

		}
		System.out.println("out");

		return list;
	}

	public static List readOpenCsv(String file) throws IOException
	{
		CSVReader reader = new CSVReader(new FileReader(file));
		List listL = reader.readAll();
		return listL;
	}

	private static ArrayList<String> intro3(String file) throws IOException
	{

		ArrayList<String> list = new ArrayList<String>();
		FileInputStream fis = new FileInputStream(file);
		byte buf[] = new byte[4096];
		int n;
		StringBuilder lineSB = new StringBuilder();
		while ((n = fis.read(buf)) != -1)
		{
			for (int i = 0; i < n; i++)
			{
				lineSB.append(buf[i]);
				if (buf[i] == '\n')
				{
					list.add(lineSB.toString());
					lineSB = new StringBuilder();
				}
			}
		}
		fis.close();
		return list;
	}

	private static ArrayList<String> bufferedInputStream(String file) throws IOException
	{

		ArrayList<String> list = new ArrayList<String>();
		FileInputStream fi = new FileInputStream(file);
		// int size = (int)fi.getChannel().size();
		InputStream fis = new BufferedInputStream(fi/* , size */);
		int c = 0;
		StringBuilder lineSB = new StringBuilder();
		while ((c = fis.read()) != -1)
		{
			lineSB.append((char) c);
			if ((char) c == '\n')
			{
				String s = lineSB.toString();
				list.add(s.substring(0, s.length() - 1));
				lineSB = new StringBuilder();
			}
		}

		return list;
	}

	private static ArrayList<String> mix01(String filename) throws IOException
	{

		ArrayList<String> list = new ArrayList<String>();

		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;

		fis = new FileInputStream(filename);

		bis = new BufferedInputStream(fis);
		dis = new DataInputStream(bis);

		while (dis.available() != 0)
		{
			list.add(dis.readLine());
		}

		fis.close();
		bis.close();
		dis.close();

		return list;
	}

	public static ArrayList<String> readB(String filename) throws IOException
	{
		InputStream istream = new FileInputStream(filename);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedReader reader = null;
		int size = (int) ((FileInputStream) istream).getChannel().size();
		byte[] buffer = new byte[size]; // Experiment with this value
		int bytesRead;
		ArrayList<String> list = new ArrayList<String>();

		while ((bytesRead = istream.read(buffer)) != -1)
		{
			baos.write(buffer, 0, bytesRead);
			reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(baos
					.toByteArray())));
			String line;

			while ((line = reader.readLine()) != null)
			{
				list.add(line);
			}
		}
		return list;

	}

	public static ArrayList<String> mix4(String filename) throws IOException
	{
		ArrayList<String> list = new ArrayList<String>();

		File file = new File(filename);
		FileInputStream inputStream = null;
		BufferedReader bufferedReader = null;
		FileChannel channel = null;
			inputStream = new FileInputStream(file);
			channel = inputStream.getChannel();
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			String data;
			while ((data = bufferedReader.readLine()) != null)
			{
				list.add(data);
			}
		channel.close();
		return list;

	}

}
